/**
 * Created by yoyoyk on 2017/3/8.
 */
import React , {Component}from 'react';
import {message } from 'antd';
import InputBox from '../../common/InputBox.js'
import Confirm from '../../common/Confirm'
import Select from '../../common/Select'
import UpdateUser from "./mutation/UpdateUserMutation"
import Relay from "react-relay";
import GetRolesMutation from "./mutation/GetRolesMutation"
import Reactkey from "../../util/ReactKey"

import {Modal} from 'fly';

/*系统后台>后台管理>后台人员列表>用户列表>编辑人员信息*/
export default class Editpersoninfo extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            visible: false,
            departname:{},
            departmentforchoose:[],
            phone:'',
            name:'',
            uid:'',
            depId:'',
            role:[],
            roleId:'',
            defaultValue:'',
            index:0,
            disable:true,
            disable1:true,
        }
        this.Style={};
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.permissionId = sessionStorage.getItem('permissionId');
    }

    componentWillMount() {
        this.Style.addpersonview={
            textAlign:"center",
            display:"block",
            border:"3px solid green"
        }
        this.Style.addpersonview.width = 400;
        this.Style.addpersonview.height = 200;
        this.Style.addpersonview.fontSize = 30;
        this.Style.addpersonview={
            width:"634px",
            height:"44px",
            paddingLeft:"18px",
            lineHeight:"44px",
            display:"block",
            background:"#f2f2f2",
            marginBottom:49
        }

    }

    state = { visible: false }
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    handleOk = (e) => {
        this.setState({
            visible: false,
        });
    }
    handleCancel = (e) => {
        this.setState({
            visible: false,
        });
        // this.props.onchose(this.state.depId,this.state.index)
        // this.props.getAllPerson();
    }
    //修改
    createuser(){
        var pwd=this.refs.password.state.text;
        if(this.state.disable1==true){
            pwd='';
        }else{
            var rs=this.validate('ratepayer');
            if(!rs){return;}
        }
        let enterpriseCode1 = this.refs.email.state.text;
        // if(enterpriseCode1!=""){
            if(!this.validate("mal")){
                return;
            }
        // }
        let roleId
        if(this.refs.selectrole.state.value==''){
            roleId = this.refs.selectrole.state.defaultValue
        }else{
            roleId = this.refs.selectrole.state.value
        }
        let depid
        if(this.refs.partname.state.value==''){
            depid =  this.refs.partname.state.defaultValue
        }else{
            depid = this.refs.partname.state.value;
        }
        Relay.Store.commitUpdate(
            new UpdateUser({
                enterpriseId: this.enterpriseId,
                userId: this.state.uid,
                depId: depid,
                roEnId: this.enterpriseId,
                roleId: roleId,
                name:this.refs.name.state.text,
                phone:this.refs.phone.state.text,
                email:this.refs.email.state.text,
                password:pwd,
            }),
            {
                onFailure: ()=>{
                    message.info('请求失败');
                },
                onSuccess: (response)=>{
                    let errorCode = response.updateUser.updateUser.errorCode;
                    let value = response.updateUser.updateUser.value
                    if(errorCode==0){
                        this.setState({
                            visible: false,
                        });
                        if(this.props.findAll==true){//如果点击全部员工
                            this.props.getAllPerson();
                        }else{
                            this.props.onchose(this.state.depId,this.state.index)
                        }
                    }
                    message.info(value);
                }
            }
        );
    }

    bluron(type){
       this.validate(type);
    }

    validate(type){
        let rs=false;
        switch (type){
            case 'mal':
                let enterpriseCode1 = this.refs.email.state.text;
                let re_enterpriseCode = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
                if(enterpriseCode1==""){
                    rs=true;
                    this.setState({
                        mal:""
                    });
                }else if(!re_enterpriseCode.test(enterpriseCode1)){
                    this.setState({
                        mal:"邮箱格式错误，请重新输入"
                    });
                    rs=false;
                }else{
                    this.enterpriseCode = enterpriseCode1;
                    rs=true;
                    this.setState({
                        mal:""
                    });
                }
                return rs;
            case 'ratepayer':
                // if(this.refs.defaultPass.checked==false){
                let taxpayerNo1 = this.refs.password.state.text;
                // let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,32}$/;
                if(taxpayerNo1.length>=6&&taxpayerNo1.length<=32){
                    this.setState({
                        ratepayer:""
                    });
                    rs=true;
                }else if(taxpayerNo1==""){
                    this.setState({
                        ratepayer:"密码不能为空",
                        disabled:false,
                });
                }else {
                    this.setState({
                        ratepayer:"密码格式为6-32位，请重新输入",
                        disabled:false,
                    });


                }
                return rs;
            // }
        }
    }
    resetPass(){
        this.refs.password.setState({
            text:''
        });
        this.setState({
            disable1:false,
            text:''
        });

    }

    render(){
        return (
            <div>
                <Modal maskClosable={false} visible={this.state.visible} className="people_addstaff---modal close_style_modal"
                       onCancel={this.handleCancel} onOk={this.createuser.bind(this)} style={{width:"634px",height:"240px",display:"block"}} title={"编辑人员信息"}
                >
                    <InputBox defaultvalute="请输入手机号" className="_input__re_margin" titletext="手机号" space="12px" style={{width:338,height:30,borderColor:"#d7d7d7",color:"#999",paddingRight:75}} ref={'phone'} disabled={this.state.disable}/>
                    <InputBox defaultvalute="请输入姓名" className="_input__re_margin" titletext="姓名" space="12px" style={{width:338,height:30,borderColor:"#d7d7d7",color:"#999",marginTop:"20px",paddingRight:75}} ref={'name'} disabled={this.state.disable}/>
                    <InputBox type="text" className="_input__re_margin" defaultvalute="请输入密码" titletext="密码" maxlength={32}  isloginintext={true} space="12px" style={{width:338,height:30,borderColor:"#d7d7d7",color:"#999",marginTop:"20px",paddingRight:75}} ref={'password'} bluron={this.bluron.bind(this,'ratepayer')} disabled={this.state.disable1}/>
                    <span style={{color:"#ff6f64",position:"absolute",left:"131px",top:"228px"}}>{this.state.ratepayer}</span>
                    <span style={{display:"block",color:"#009dd9",marginLeft:65,marginTop:6}}><a href="javascript:;" onClick={this.resetPass.bind(this)}>重置密码</a></span>

                    <InputBox titletext="邮箱" maxlength={30} className="_input__re_margin" defaultvalute="请输入邮箱" space="12px" style={{width:338,height:30,borderColor:"#d7d7d7",color:"#999",marginTop:"20px",paddingRight:75}} ref={'email'} bluron={this.bluron.bind(this,'mal')}/>

                    <span style={{color:"#ff6f64",position:"absolute",left:"73px",top:"297px"}}>{this.state.mal}</span>
                    <div style={{marginTop:20,marginLeft:22}} className="people_muchmodifypart--select nth_sel_2">
                        <span style={{marginRight:6}} > 部门 </span>
                        <Select oArr={this.state.departname} style={{width:349,height:30,display:"inlineBlock"}} ref={'partname'} defaultValue={this.state.depId} key={Reactkey.key}/>
                    </div>
                    <div style={{marginTop:20,marginLeft:22}} className="people_muchmodifypart--select nth_sel_2">
                        <span style={{marginRight:5}}> 职务 </span>
                        <Select  oArr={this.state.role}  style={{width:349,height:30,display:"inlineBlock"}} ref={'selectrole'} defaultValue={this.state.roleId} key={Reactkey.key}/>
                    </div>
                </Modal>
            </div>
        )
    }
}



